import request from '@/utils/request'

// 获取问题列表
export function getQuestions(params) {
  return request({
    url: '/api/questions',
    method: 'get',
    params
  }).then(response => {
    // 添加调试日志，查看从服务器返回的原始数据
    console.log('从服务器获取的原始问题数据:', response);
    
    if (response.code === 200 && response.data) {
      // 检查数据结构
      const records = response.data.list || response.data.records || [];
      if (Array.isArray(records)) {
        // 检查每个题目的分数字段
        records.forEach((question, index) => {
          console.log(`题目${index + 1}(ID: ${question.id})原始分数字段:`, {
            aEScore: question.aEScore, aIScore: question.aIScore,
            aSScore: question.aSScore, aNScore: question.aNScore,
            aTScore: question.aTScore, aFScore: question.aFScore,
            aJScore: question.aJScore, aPScore: question.aPScore,
            bEScore: question.bEScore, bIScore: question.bIScore,
            bSScore: question.bSScore, bNScore: question.bNScore,
            bTScore: question.bTScore, bFScore: question.bFScore,
            bJScore: question.bJScore, bPScore: question.bPScore
          });
          
          // 检查分数字段的类型
          const scoreTypes = {
            aEScore: typeof question.aEScore, aIScore: typeof question.aIScore,
            aSScore: typeof question.aSScore, aNScore: typeof question.aNScore,
            aTScore: typeof question.aTScore, aFScore: typeof question.aFScore,
            aJScore: typeof question.aJScore, aPScore: typeof question.aPScore,
            bEScore: typeof question.bEScore, bIScore: typeof question.bIScore,
            bSScore: typeof question.bSScore, bNScore: typeof question.bNScore,
            bTScore: typeof question.bTScore, bFScore: typeof question.bFScore,
            bJScore: typeof question.bJScore, bPScore: typeof question.bPScore
          };
          console.log(`题目${index + 1}(ID: ${question.id})分数字段类型:`, scoreTypes);
        });
      }
    }
    
    return response;
  });
}

// 创建问题
export function createQuestion(data) {
  return request({
    url: '/api/questions',
    method: 'post',
    data
  })
}

// 更新问题
export function updateQuestion(id, data) {
  console.log('准备更新题目，ID:', id, '更新数据:', data);
  return request({
    url: `/api/questions/${id}`,
    method: 'put',
    data
  }).then(response => {
    console.log('更新题目响应:', response);
    return response;
  }).catch(error => {
    console.error('更新题目错误:', error);
    throw error;
  });
}

// 删除问题
export function deleteQuestion(id) {
  return request({
    url: `/api/questions/${id}`,
    method: 'delete'
  })
}

// 获取单个问题详情
export function getQuestion(id) {
  return request({
    url: `/api/questions/${id}`,
    method: 'get'
  }).then(response => {
    // 确保所有字段名称格式一致，同时保留原始字段名
    if (response.code === 200 && response.data) {
      const data = response.data;
      
      // 创建一个将字符串转为数字的辅助函数
      const toNumber = (value) => {
        if (value === null || value === undefined) return 0;
        return typeof value === 'string' ? Number(value) : value;
      };
      
      // 创建一个新对象，保留原始字段并添加驼峰式字段名
      const convertedData = {
        ...data,
        // 保留原始小写字段名，确保它们是数字类型
        aescore: toNumber(data.aescore),
        aiscore: toNumber(data.aiscore),
        asscore: toNumber(data.asscore),
        anscore: toNumber(data.anscore),
        atscore: toNumber(data.atscore),
        afscore: toNumber(data.afscore),
        ajscore: toNumber(data.ajscore),
        apscore: toNumber(data.apscore),
        bescore: toNumber(data.bescore),
        biscore: toNumber(data.biscore),
        bsscore: toNumber(data.bsscore),
        bnscore: toNumber(data.bnscore),
        btscore: toNumber(data.btscore),
        bfscore: toNumber(data.bfscore),
        bjscore: toNumber(data.bjscore),
        bpscore: toNumber(data.bpscore),
        
        // 添加驼峰式字段名，确保它们是数字类型
        aEScore: toNumber(data.aEScore !== undefined ? data.aEScore : data.aescore),
        aIScore: toNumber(data.aIScore !== undefined ? data.aIScore : data.aiscore),
        aSScore: toNumber(data.aSScore !== undefined ? data.aSScore : data.asscore),
        aNScore: toNumber(data.aNScore !== undefined ? data.aNScore : data.anscore),
        aTScore: toNumber(data.aTScore !== undefined ? data.aTScore : data.atscore),
        aFScore: toNumber(data.aFScore !== undefined ? data.aFScore : data.afscore),
        aJScore: toNumber(data.aJScore !== undefined ? data.aJScore : data.ajscore),
        aPScore: toNumber(data.aPScore !== undefined ? data.aPScore : data.apscore),
        bEScore: toNumber(data.bEScore !== undefined ? data.bEScore : data.bescore),
        bIScore: toNumber(data.bIScore !== undefined ? data.bIScore : data.biscore),
        bSScore: toNumber(data.bSScore !== undefined ? data.bSScore : data.bsscore),
        bNScore: toNumber(data.bNScore !== undefined ? data.bNScore : data.bnscore),
        bTScore: toNumber(data.bTScore !== undefined ? data.bTScore : data.btscore),
        bFScore: toNumber(data.bFScore !== undefined ? data.bFScore : data.bfscore),
        bJScore: toNumber(data.bJScore !== undefined ? data.bJScore : data.bjscore),
        bPScore: toNumber(data.bPScore !== undefined ? data.bPScore : data.bpscore)
      };
      
      console.log("转换后的题目数据:", convertedData);
      response.data = convertedData;
    }
    return response;
  });
} 